package com.hllxd.hism.star.basic.service.service.alarm;

import com.hllxd.hism.star.basic.service.constant.alarm.HandleStrategyEnum;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.AlarmListHandleMapper;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.AlarmRuleMapper;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.PauseConfigMapper;
import com.hllxd.hism.star.basic.service.model.alarm.*;
import com.hllxd.hism.star.basic.service.util.DateUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author wuguangxi
 * @description
 * @date 2022/3/25 14:21
 **/
@Service
public class AlarmListHandleService {

    @Autowired
    private AlarmListHandleMapper alarmListHandleMapper;
    @Autowired
    private AlarmListService alarmListService;
    @Autowired
    private AlarmRuleMapper alarmRuleMapper;
    @Autowired
    private PauseConfigMapper pauseConfigMapper;


    public void handleAlarm(String alarmId, String oa) throws Exception{
        addHandleRecord(alarmId, oa);
        addPause(alarmId, oa);
    }

    private void addPause(String alarmId, String oa) throws Exception{
        AlarmList alarmList = alarmListService.findByAlarmId(alarmId);
        if(alarmList == null){
            return;
        }
        Integer ruleId = alarmList.getRuleId();
        AlarmRule alarmRule =  alarmRuleMapper.findByRuleId(ruleId);
        if(alarmRule == null){
            return;
        }
        String handleStrategy = alarmRule.getHandleStrategy();
        HandleStrategyEnum handleStrategyEnum = HandleStrategyEnum.getByCode(handleStrategy);
        if(handleStrategyEnum == null){
            return;
        }
        // 根据策略，设置暂不告警的时间，构造不告警对象
        addPauseConfig(alarmRule, alarmList, oa, handleStrategyEnum);
    }

    private void addHandleRecord(String alarmId, String oa){
        alarmId = "alarmId";//SecuritySdk.sqlEscape(alarmId);
        AlarmListHandle alarmListHandle = new AlarmListHandle();
        alarmListHandle.setAlarmId(alarmId);
        alarmListHandle.setHandleUser(oa);
        alarmListHandle.setHandleTime(DateUtil.formatDateTime(System.currentTimeMillis()));
        alarmListHandleMapper.insert(alarmListHandle);
        alarmListService.updateStatus(alarmId);
    }

    private void addPauseConfig(AlarmRule alarmRule, AlarmList alarmList, String oa, HandleStrategyEnum handleStrategyEnum) throws Exception{
        List<AlarmErrorBO> alarmErrorBOList = new Gson().fromJson(alarmList.getAlarmDetail(), new TypeToken<List<AlarmErrorBO>>(){}.getType());
        String minTime = DateUtil.formatDateTime(System.currentTimeMillis());
        Calendar calendar = Calendar.getInstance();
        switch (handleStrategyEnum){
            case ONE_HOUR:
                calendar.add(Calendar.HOUR, 1);
                break;
            case SIX_HOUR:
                calendar.add(Calendar.HOUR, 6);
                break;
            case TWELVE_HOUR:
                calendar.add(Calendar.HOUR, 12);
                break;
            case ONE_DAY:
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                break;
        }
        String maxTime = DateUtil.formatDateTime(calendar.getTime().getTime());

        Set<String> keys = new HashSet<>();
        alarmErrorBOList.forEach(error -> {
            PauseConfig pauseConfig = new PauseConfig();
            pauseConfig.setTopicId(alarmRule.getTopicId());
            pauseConfig.setMatchType("accurate");
            pauseConfig.setCreator(oa);
            pauseConfig.setCreateTime(DateUtil.formatDateTime(System.currentTimeMillis()));
            pauseConfig.setAlarmType(error.getAlarmType() + "");
            pauseConfig.setTopicDimComposeId(Integer.parseInt(alarmRule.getTopicDimComposeId()));
            String dimCombine = error.getDimCombine();
            String dimDescartes = error.getDimDescartes();
            List<Map<String, String>> dimValueList = new ArrayList<>();
            String[] dimCombineArr = dimCombine.split(",");
            String[] dimDescartesArr = dimDescartes.split(",");
            for (int i = 0; i < dimCombineArr.length; i++) {
                Map<String, String> map = new HashMap<>();
                map.put("dim", dimCombineArr[i]);
                map.put("value", dimDescartesArr[i]);
                dimValueList.add(map);
            }
            pauseConfig.setDimValue(new Gson().toJson(dimValueList));
            pauseConfig.setMaxTime(maxTime);
            pauseConfig.setMinTime(minTime);
            String key = pauseConfig.getTopicId() + pauseConfig.getAlarmType() + pauseConfig.getTopicDimComposeId() + pauseConfig.getDimValue();
            if(keys.contains(key)){
                return;
            }
            keys.add(key);
            pauseConfigMapper.insert(pauseConfig);
        });
    }
}
